package cn.edu.baiyunu.myapplication.data.db.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import cn.edu.baiyunu.myapplication.data.db.entity.PhotoRecord
import kotlinx.coroutines.flow.Flow

@Dao
interface PhotoRecordDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(photoRecord: PhotoRecord): Long
    
    @Update
    suspend fun update(photoRecord: PhotoRecord)
    
    @Delete
    suspend fun delete(photoRecord: PhotoRecord)
    
    @Query("DELETE FROM photo_records WHERE id = :id")
    suspend fun deleteById(id: Long)
    
    @Query("DELETE FROM photo_records")
    suspend fun deleteAll()
    
    @Query("SELECT * FROM photo_records ORDER BY createdAt DESC")
    fun getAllPhotoRecords(): Flow<List<PhotoRecord>>
    
    @Query("SELECT * FROM photo_records WHERE id = :id")
    fun getPhotoRecordById(id: Long): Flow<PhotoRecord?>
    
    @Query("SELECT * FROM photo_records WHERE specType = :specType ORDER BY createdAt DESC")
    fun getPhotoRecordsBySpecType(specType: String): Flow<List<PhotoRecord>>
    
    @Query("SELECT * FROM photo_records WHERE specType LIKE '%' || :query || '%' ORDER BY createdAt DESC")
    fun searchPhotoRecords(query: String): Flow<List<PhotoRecord>>
    
    @Query("SELECT * FROM photo_records ORDER BY createdAt DESC LIMIT :limit")
    fun getRecentPhotoRecords(limit: Int): Flow<List<PhotoRecord>>
    
    @Query("SELECT * FROM photo_records WHERE id = :id")
    suspend fun getPhotoById(id: Long): PhotoRecord?
} 